package com.example.kuaizu.service.imp;

import com.example.kuaizu.mapper.EmailPromotionMapper;
import com.example.kuaizu.mapper.InterestedMapper;
import com.example.kuaizu.pojo.Promotion;
import com.example.kuaizu.pojo.dto.UserEmailDao;
import com.example.kuaizu.pojo.ov.PromotionArrayObjectOv;
import com.example.kuaizu.service.InInterestedService;
import com.example.kuaizu.utils.SerializerUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Log4j2
@Service
public class InterestedService implements InInterestedService {
    @Autowired
    InterestedMapper interestedMapper;
    @Autowired
    private EmailPromotionMapper emailPromotionMapper;
    //根据项目id查询可能感兴趣的人
    @Override
    @Transactional
    public Set<UserEmailDao> getInterestedUser(Integer projectId) {
        List<Promotion> emailPromotionParms = interestedMapper.selectPromotionInfo(projectId);
        //反序列化promotionArrayObjectsOv
        Set<UserEmailDao> userList = new HashSet<>();
        for (Promotion promotion : emailPromotionParms) {
            //反序列化 成promotionArrayObjectsOv
            try {
                PromotionArrayObjectOv paov =(PromotionArrayObjectOv) SerializerUtils.deserializeFromBase64(promotion.getDescription());
                //查询到用户的信息
                List<UserEmailDao> userEmailDaos = emailPromotionMapper.selectEmailBySchoolAndMajor(paov.getPromotionArrayObject());
                userList.addAll(userEmailDaos);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        log.info("查询到的用户信息为：{}",userList);
        return userList;
    }
}
